package com.tang.community.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.community.Comment;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.community.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/commentList")
    public Result getCommentList(String keywords, Integer currentPage, Integer pageSize) {
        PageInfo<Comment> list = commentService.listComment(keywords,currentPage,pageSize);
        List li =  list.getList();

        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/getMyCommentList")
    public Result getMyCommentList(String userName, Integer currentPage, Integer pageSize) {
        PageInfo<Comment> list = commentService.listMyComment(userName,currentPage,pageSize);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/publishComment")
    public Result publishComment(@RequestParam("form") @RequestBody String form) {
        Comment comment = JSON.parseObject(form, Comment.class);
       if(comment!=null){
           boolean save = commentService.save(comment);
           if(save){
               return Result.success("发表成功");
           }
       }
       return Result.error(RecruitExceptionEnum.PUBLISH_FAILED);
    }


    @RequestMapping("/articleComments")
    public Result getCommentList(Integer articleId, Integer currentPage, Integer pageSize) {
        PageInfo<Comment> list = commentService.listArticleComments(articleId,currentPage,pageSize);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/delComment")
    public Result delCommentById(String commentId) {

        Comment comment = commentService.getById(commentId);
        comment.setDeleteFlag(0);
        boolean result = commentService.updateById(comment);
        if(result){
            return Result.success("删除成功");
        }else{
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }
    @RequestMapping("/nestComment")
    public Result getNestComment(String articleTitle) {
    List<Comment> list =  commentService.getNestcomment(articleTitle);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/commentCount")
    public Integer getCommentCount(){
        return commentService.count(null);
    }
}
